/*
 */
package org.vectrics.common.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;


/**
 * @author mmoore
 */
public class AuditInfoType { //implements UserType {
    private static final int[] SQL_TYPES = new int[]{
        Types.TIMESTAMP,
        Types.TIMESTAMP,
        Types.BIGINT,
        Types.BIGINT
    };

    public int[] sqlTypes() {
        return SQL_TYPES;
    }

    public boolean isMutable() {
        return true;
    }

    public Class returnedClass() {
        return AuditInfo.class;
    }

    public boolean equals(Object x, Object y) {
        if( x == y ) return true;
        if( x == null || y == null ) return false;
        AuditInfo aix = (AuditInfo) x;
        AuditInfo aiy = (AuditInfo) y;
        try {
            return Hibernate.TIMESTAMP.isEqual(aix.getLastUpdated(), aiy.getLastUpdated()) &&
                    Hibernate.TIMESTAMP.isEqual(aix.getCreated(), aiy.getCreated()) &&
                    Hibernate.LONG.isEqual(aix.getUpdatedBy(), aiy.getUpdatedBy()) &&
                    Hibernate.LONG.isEqual(aix.getCreatedBy(), aiy.getCreatedBy());
        } catch(Exception e ) {
            return false;
        }
    }

    public Object deepCopy(Object value) {
        if( value == null ) return null;
        AuditInfo ai = (AuditInfo) value;
        AuditInfo result = new AuditInfo();
        try {
            result.setLastUpdated(ai.getLastUpdated());
            result.setCreated(ai.getCreated());
            result.setUpdatedBy(ai.getUpdatedBy());
            result.setCreatedBy(ai.getCreatedBy());
            return result;
        } catch( Exception e ) {
            e.printStackTrace();
            return result;
        }
    }

    public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
            throws HibernateException, SQLException {

        //AuditInfo can't be null
        AuditInfo ai = new AuditInfo();
        System.out.println(Hibernate.TIMESTAMP.nullSafeGet(rs, names[0]).getClass()) ;
        ai.setLastUpdated((java.sql.Timestamp) Hibernate.TIMESTAMP.nullSafeGet(rs, names[0]));
        ai.setCreated((java.sql.Timestamp) Hibernate.TIMESTAMP.nullSafeGet(rs, names[1]));
        ai.setUpdatedBy((Long) Hibernate.LONG.nullSafeGet(rs, names[2]));
        ai.setCreatedBy((Long) Hibernate.LONG.nullSafeGet(rs, names[3]));
        return ai;
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index)
            throws HibernateException, SQLException {

        //AuditInfo can't be null
        AuditInfo ai = (AuditInfo) value;
        Hibernate.TIMESTAMP.nullSafeSet(st, ai.getLastUpdated(), index);
        Hibernate.TIMESTAMP.nullSafeSet(st, ai.getCreated(), index + 1);
        Hibernate.LONG.nullSafeSet(st, ai.getUpdatedBy(), index + 2);
        Hibernate.LONG.nullSafeSet(st, ai.getCreatedBy(), index + 3);
    }
}

